<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

//Route::get('/', 'HomeController@showWelcome');
Route::get('/', function() {
	if (Auth::guest())
		return View::make("login");
	else
		return Redirect::to('home');
});

// Authentication
Route::get('login', 'AuthController@showLogin');
Route::post('login', 'AuthController@postLogin');
Route::get('logout', 'AuthController@getLogout');

Route::group(array('before'=>'api'), function() { 
	Route::post('upload', 'UploadController@index');
});

Route::group(array('before'=>'auth'), function() { 

	Route::get('home', 'HomeController@showTree');
	
	Route::get('generatePDF/{id}', 'HomeController@generatePDF');
	Route::get('collapse', 'HomeController@showCollapse');
	Route::get('tcpdf', 'HomeController@showTCPDF');
	Route::get('gridSystem', 'HomeController@showGirdSystem');
	
	Route::resource('users', 'UserController');
	Route::get('users/{id}/delete', 'UserController@delete');
	Route::resource('departments', 'DepartmentController');
	Route::get('departments/{id}/delete', 'DepartmentController@delete');
	Route::resource('category', 'CategoryController');
	Route::get('category/{id}/delete', 'CategoryController@delete');
	Route::resource('templates', 'TemplateController');
	
	Route::get('treeContent', 'HomeController@showSecret');	
	Route::get('treeContent2', 'HomeController@treeContent2');	
	
	Route::get('scanDocument', 'TagDocumentController@showScanDocument');
	Route::get('scanDocumentS2', 'TagDocumentController@showScanDocumentS2');
	Route::post('scanDocumentS2', 'TagDocumentController@showScanDocumentS3');
	Route::post('scanDocumentS3/{id}', 'TagDocumentController@saveScanDocument');
	
	Route::get('loadTemplate/{id}', 'SearchDocController@loadTemplate');
	Route::post('searchDocument', 'SearchDocController@searchDocument');
	
});

View::composer('users.edit', function($view)
{
	$roles = Role::all();
	if(count($roles) > 0){
		$role_options = array_combine($roles->lists('id'), $roles->lists('role_name'));
	} else {
		$role_options = array(null, 'Unspecified');
	}
	$departments = Department::all();
	if(count($departments) > 0){
		$department_options = array_combine($departments->lists('id'), $departments->lists('department_name'));
	} else {
		$department_options = array(null, 'Unspecified');
	}
	$view->with('role_options', $role_options)
		 ->with('department_options', $department_options);
});

View::composer('category.edit', function($view)
{
	$categories = Category::all();
	if(count($categories) > 0){
		$category_options = '"' . implode('","', $categories->lists('name')) . '"';
	} else {
		$category_options = '';
	}
	$view->with('category_options', $category_options);
	
});

View::composer('departments.edit', function($view)
{
	$viewdata= $view->getData();

	$department = $viewdata['department'];
	
	$listOfId = $department->templates->lists('id');

	if (count($listOfId) == 0) {
		$templates = Template::all();
	} else {
		$templates = Template::whereNotIn('id', $listOfId);
	}

	if(count($templates) > 0){
		$template_options = '"' . implode('","', $templates->lists('template_name')) . '"';
	} else {
		$template_options = '';
	}
	$view->with('template_options', $template_options);
	
});

View::composer('tree.index', function($view)
{
	$category = Category::where('parent_id', '=', '0')->first();
	$treeData = '[{"title":"' . $category->name . '", "key":"' . $category->id . '", "expanded": true, "folder": true, "children": ['.HomeController::jsonList2($category).'] } ] ';
	
	$view->with('treeData', $treeData);
});